package com.lzxuni.modules.soilmoisture.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.poi.ExcelUtil;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.soilmoisture.entity.TblSoilMoisture;
import com.lzxuni.modules.soilmoisture.mapper.TblSoilMoistureMapper;
import com.lzxuni.modules.soilmoisture.service.TblSoilMoistureService;
import com.lzxuni.modules.weather.entity.WeatherInfo;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 墒情 服务实现类
 * </p>
 *
 * @author rdf
 * @since 2020-12-01
 */
@Service
public class TblSoilMoistureServiceImpl extends ServiceImpl<TblSoilMoistureMapper, TblSoilMoisture> implements TblSoilMoistureService {


    @Override
    public PageInfo<TblSoilMoisture> queryPage(PageParameter pageParameter, TblSoilMoisture tblSoilMoisture) {
        if (StringUtils.isNotEmpty(pageParameter.getSidx())) {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                    pageParameter.getSidx() + " " + pageParameter.getSord());
        } else {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows());
        }
        List<TblSoilMoisture> tblSoilMoistureList = baseMapper.queryList(tblSoilMoisture);
        PageInfo<TblSoilMoisture> pageInfo = new PageInfo<>(tblSoilMoistureList);
        return pageInfo;
    }
    //导出
    @Override
    public Workbook exportExcel(TblSoilMoisture tblSoilMoisture) throws IOException {
        PageHelper.orderBy("create_time" + " " + "DESC");
        LinkedHashMap<String, List<List<String>>> dataMap = new LinkedHashMap<>();
        List<List<String>> listData1 = new ArrayList<>();
        Workbook wb = null;
        List<TblSoilMoisture> tblSoilMoistureList =baseMapper.queryList(tblSoilMoisture);
        List<String> list0 = new ArrayList<>();
        list0.add("所属地块");
        list0.add("日期");
        list0.add("土壤湿度");
        list0.add("温度级别");
        list0.add("地温");
        listData1.add(list0);

        if (tblSoilMoistureList != null && tblSoilMoistureList.size() > 0) {
            for (int i = 0; i < tblSoilMoistureList.size(); i++) {
                TblSoilMoisture tblSoilMoisture1 = tblSoilMoistureList.get(i);
                List<String> list = new ArrayList<>();

                list.add(tblSoilMoisture1.getLandNum());
                list.add(DateFormatUtils.format(tblSoilMoisture1.getCreateTime(),"yyyy-MM-dd"));
                list.add(tblSoilMoisture1.getSoilHumidity().toString());
                list.add(tblSoilMoisture1.getSoilTelevel());
                list.add(tblSoilMoisture1.getSoilTemperature().toString());
                listData1.add(list);
            }
        }
        dataMap.put("墒情管理", listData1);
        ExcelUtil excelUtil = new ExcelUtil();
        wb = excelUtil.writeExcel(dataMap, "墒情管理");
        return wb;
    }

    //墒情温度,湿度月分析
    @Override
    public List<TblSoilMoisture> queryMonthList(TblSoilMoisture tblSoilMoisture) {
        return baseMapper.queryMonthList(tblSoilMoisture);
    }

    //墒情温度,湿度月年分析
    @Override
    public List<TblSoilMoisture> queryYearList(TblSoilMoisture tblSoilMoisture) {
        return baseMapper.queryYearList(tblSoilMoisture);
    }
}
